package com.bestcem.xm.txtanls.controller.param.chart;

import cn.hutool.core.lang.Validator;
import com.bestcem.xm.common.core.constant.Constants;
import com.bestcem.xm.txtanls.annotation.EnumValueValidate;
import com.bestcem.xm.txtanls.enums.TopicTypeEnum;
import com.bestcem.xm.txtanls.grpc.service.param.chart.BaseChartParam;
import com.bestcem.xm.txtanls.grpc.service.param.chart.TopicReferenceParam;
import com.bestcem.xm.txtanls.grpc.v1.enums.SortKeyEnum;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import javax.validation.constraints.NotNull;
import java.time.LocalDate;

/**
 * 话题引用参数
 * @author jy.zhao
 * @version 2.0
 * @date 2021/8/5 18:03
 **/
@Data
@AllArgsConstructor
public class TopicReferenceQuery extends BaseChartQuery {

    @ApiModelProperty(value = "话题组id")
    @JsonProperty(value = "topics_group_id")
    private String topicGid;

    @NotNull(message = "话题类型不能为空")
    @EnumValueValidate(enumClass = TopicTypeEnum.class)
    @ApiModelProperty(value = "话题类型不能为空,GROUP、TOPIC。默认GROUP", example = "GROUP")
    @JsonProperty(value = "topics_type")
    private String topicType;

    @ApiModelProperty(value = "排序规则,PREVIOUS、CURRENT、COMPARE_PREVIOUS。默认COMPARE_PREVIOUS", example = "COMPARE_PREVIOUS")
    @JsonProperty(value = "sort")
    private String sortKey;

    public TopicReferenceQuery(String projectId, String fieldId, String filterType, String current, LocalDate startTime, LocalDate endTime, String topicGid, String topicType, String sortKey) {
        super(projectId, fieldId, filterType, current, startTime, endTime);
        this.topicGid = topicGid;
        this.topicType = topicType;
        this.sortKey = sortKey;
    }

    public TopicReferenceParam getTopicReferenceParam() {
        BaseChartParam baseChartParam = super.getBaseChartParam();
        TopicReferenceParam topicReferenceParam = new TopicReferenceParam();
        BeanUtils.copyProperties(baseChartParam, topicReferenceParam);
        topicReferenceParam.setTopicGid(this.topicGid);
        // 设置话题类型
        if(Validator.isEmpty(this.topicType)) {
            topicReferenceParam.setTopicType(TopicTypeEnum.GROUP.getIndex());
        } else if(Constants.TOPIC_GROUP.equals(this.topicType.toUpperCase())) {
            topicReferenceParam.setTopicType(TopicTypeEnum.GROUP.getIndex());
        } else if(Constants.TOPIC.equals(this.topicType.toUpperCase())) {
            topicReferenceParam.setTopicType(TopicTypeEnum.TOPIC.getIndex());
        }
        // 设置排序规则
        if(Validator.isEmpty(this.sortKey)) {
            topicReferenceParam.setSortKey(SortKeyEnum.SortKey.COMPARE_PREVIOUS.name());
        } else {
            topicReferenceParam.setSortKey(this.sortKey.toUpperCase());
        }
        return topicReferenceParam;
    }
}
